package logic

import (
	"context"
	"hm-zero-mall/apps/address/models"

	"hm-zero-mall/apps/address/pb"
	"hm-zero-mall/apps/address/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAddressLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetAddressLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAddressLogic {
	return &GetAddressLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetAddress 获取地址
func (l *GetAddressLogic) GetAddress(in *pb.AddressIdReq) (*pb.AddressInfo, error) {
	address, err := l.svcCtx.AddressModel.FindOne(l.ctx, in.Id)
	if err != nil {
		return nil, err
	}
	if address.Id == 0 {
		return nil, models.ErrNotFound
	}

	return &pb.AddressInfo{
		Uid:       address.UserId,
		Province:  address.Province,
		City:      address.City,
		Town:      address.Town,
		Street:    address.Street,
		Mobile:    address.Mobile,
		Contact:   address.Contact,
		Notes:     address.Notes,
		IsDefault: address.IsDefault,
	}, nil
}
